import {
	request
} from '@/common/request/index.js'
import localStorage from '@/common/localstorage/localstorage.js'

function checkLoginState(wanttoUrl) {
	let wanttoUrlCopy = encodeURIComponent(wanttoUrl)
	let noLogin = () => {
		let jumpUrl = ''
		// #ifdef H5
		jumpUrl = '../../pages/login/common'
		// #endif
		// #ifndef H5
		jumpUrl = '../../pages/login/quick'
		// #endif
		uni.navigateTo({
			url: jumpUrl + '?redirect=' + wanttoUrlCopy
		})
	}
	let successCall = (resdata) => {
		request.get('/client.ashx', {
				data: {
					c: 'UserCenter',
					m: 'CheckLoginStatus',
					sessionid: resdata.sessionid,
					userid: resdata.userid
				}
			})
			.then((res) => {
				let result = res.data;
				if (result.Status == 'error' && result.Msg.indexOf('用户会话状态无效') > -1) {
					noLogin();
				} else {
					uni.navigateTo({
						url: wanttoUrl
					})
				}
			})
			.catch(error => {
				noLogin();
				console.error('error:', error);
			});
	}
	let p = {
		key: 'userinfokey'
	}
	let d = localStorage.getStorageSync(p);
	if (d)
		successCall(d)
	else noLogin();
}

export default checkLoginState

